package cn.wsxiot.display.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义ThreadPoolTaskExecutor
 */
@Component
public class ExecutorConfig {

    //核心线程数
    private int corePoolSize = 10;
    //最大线程数
    private int maxPoolSize = 200;
    //队列最大长度
    private int queueCapacity = 10;

    @Bean
    public Executor myAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        //线程池策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("myExecutor");
        executor.initialize();
        return executor;
    }
}